local cell = require "cell"
local redis = require "db.redis"

local conf = {
    host = "192.168.1.6",
    port = 6379,
    db = 0,
    auth = "123456"
}

local function watching()
    local w = redis.watch(conf)
    w:subscribe "foo"
    w:psubscribe "hello.*"
    while true do
        print("watch", w:message())
    end
end

function cell.main()
    cell.fork(watching)
    local db = redis.connect(conf)

    db:del "C"
    db:set("A", "hello")
    print("cell.main")
    db:set("B", "world")
    db:sadd("C", "one")

    print(db:get("A"))
    print(db:get("B"))

    db:del "D"
    for i = 1, 10 do
        db:hset("D", i, i)
    end
    local r = db:hvals "D"
    for k, v in pairs(r) do
        print(k, v)
    end

    db:multi()
    db:get "A"
    db:get "B"
    local t = db:exec()
    for _, v in ipairs(t) do
        print("Exec", v)
    end

    print(db:exists "A")
    print(db:get "A")
    print(db:set("A", "hello world"))
    print(db:get("A"))
    print(db:sismember("C", "one"))
    print(db:sismember("C", "two"))

    print("===========publish============")

    for i = 1, 10 do
        db:publish("foo", i)
    end
    for i = 11, 20 do
        db:publish("hello.foo", i)
    end

    db:disconnect()
end
